package com.hongyuan.sellfitness.ui.find.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.AppViewModelFactory;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomFragment;
import com.hongyuan.sellfitness.databinding.FragmentFindChildBinding;
import com.hongyuan.sellfitness.ui.find.view_mode.FindChildViewModel;

import me.tatarka.bindingcollectionadapter2.BR;

public class FindChildFragment extends CustomFragment<FragmentFindChildBinding , FindChildViewModel> {

    private int position;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_find_child;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public FindChildViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(FindChildViewModel.class);
    }

    @Override
    public void initParam() {
        super.initParam();

        position = Integer.parseInt(getPosition());
    }

    @Override
    public void initView() {
        super.initView();

        setOnRefresh(binding.refresh,REFRESH_0X3);

        setEnableLoadMore(Constants.GET_CIRCLE_LIST);
        setEnableRefresh(Constants.GET_CIRCLE_LIST);
    }

    @Override
    public void initData() {
        super.initData();

        viewModel.clearParams().setParams("circle_state","1");
        //.setParams("city_name",mActivity.userToken.getRegion_name());

        if(position == 0){
            viewModel.setParams("is_tj","1");
        }else if(position == 1){
            viewModel.setParams("circle_type","gz");
            viewModel.requestData(Constants.GET_MY_FRIENDS);
        }

        viewModel.requestData(Constants.GET_CIRCLE_LIST);

    }

    @Override
    public void onResume() {
        super.onResume();

        if(position == 1){
            //当为关注模块时，要更新数据
            viewModel.requestData(Constants.GET_MY_FRIENDS);
            viewModel.requestData(Constants.GET_CIRCLE_LIST);
        }
    }
}
